package com.socialcoco.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.socialcoco.VO.SocialUser;
import com.socialcoco.VO.telUserVo;
import com.socialcoco.entity.User;
import com.socialcoco.respone.ResultUtils;
import com.socialcoco.service.ShiroService;
import com.socialcoco.service.loginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
public class LoginServiceImpl implements loginService {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private ShiroServiceImpl shiroService;
    //封装手机登录
    @Override
    public User telLogin(telUserVo telUserVo) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("tel",telUserVo.getPhone());
        User one = userService.getOne(queryWrapper);
        Map result=new HashMap();
        if(one!=null){
            //这里进行登录操作
            User tel = shiroService.findTel(telUserVo.getPhone());
            if(tel!=null){
                return one;
            }else {
                return null;
            }
        }
        else {
            //第一次登录进行注册操作
            User u=new User();
            u.setHeadimgae("http://182.92.64.139:9000/movies/head.jpg");
            u.setTel(telUserVo.getPhone());
            u.setCreateTime(new Date());
            u.setUsername("普通用户");
            userService.save(u);
            return u;
        }

    }

    @Override
    public User weiboLogin(SocialUser socialUser) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("weiboid",socialUser.getUid());
        User one = userService.getOne(queryWrapper);
        Map result=new HashMap();
        if(one!=null){
            //这里进行登录操作
            User tel = shiroService.findWeibo(socialUser.getUid());
            if(tel!=null){
                return one;
            }else {
                return null;
            }
        }
        else {
            //第一次登录进行注册操作
            User u=new User();
            u.setHeadimgae(socialUser.getHeadUrl());
            u.setSex(socialUser.getGender());
            u.setCreateTime(new Date());
            u.setUsername(socialUser.getIsRealName());
            u.setWeiboid(socialUser.getUid());
            u.setAccess_token(socialUser.getAccess_token());
            userService.save(u);
            return u;
        }
    }
}
